package com.ruoyi.user.controller.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SpffVideos;
import com.ruoyi.user.service.IVideoSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 视频搜索Controller
 * 
 * @author xjj
 * @date 2025-01-28
 */
@RestController
@RequestMapping("/userApi/videos/search")
public class UVideoSearchController extends BaseController
{
    @Autowired
    private IVideoSearchService videoSearchService;

    /**
     * 搜索视频
     * 支持关键词、分类筛选
     */
    @GetMapping
    public TableDataInfo searchVideos(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "sortBy", defaultValue = "hot") String sortBy,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize)
    {
        // 如果没有任何搜索条件，返回热门视频
        if (StringUtils.isEmpty(keyword) && categoryId == null) {
            sortBy = "hot";
        }

        // 执行搜索
        List<SpffVideos> list = videoSearchService.searchVideos(
            keyword, categoryId, null, null, null, sortBy, pageNum, pageSize
        );

        return getDataTable(list);
    }
}